<template>
  <div>
    <el-dialog
      title="注册"
      :visible.sync="dialogFormVisible"
      width="20%"
      center
      @close="closeDia"
    >
      <el-form
        :model="registerForm"
        :rules="sterFormRules"
        ref="registerFormRef"
        status-icon
      >
        <!-- 账号 -->
        <el-form-item prop="userName">
          <el-input
            v-model="registerForm.userName"
            prefix-icon="el-icon-user-solid"
            placeholder="请输入账号"
          ></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input
            type="password"
            v-model="registerForm.password"
            placeholder="请输入密码"
            prefix-icon="el-icon-view"
          ></el-input>
        </el-form-item>
        <!-- 再次输入密码 -->
        <el-form-item prop="checkPass">
          <el-input
            type="password"
            v-model="registerForm.checkPass"
            placeholder="请再次输入密码"
            prefix-icon="el-icon-view"
          ></el-input>
        </el-form-item>
        <!-- 注册 -->
        <el-form-item>
          <el-button type="primary" @click="addRegister" style="width: 100%"
            >注册</el-button
          >
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import axios from "../api/loginOnRegApi";
export default {
  name: "Register",
  data() {
    // 名字验证
    var checkAge = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("请输入用户名"));
      }
      if (!/^\w{5,16}[0-9,A-z]/.test(value)) {
        callback(new Error("字母开头,长度5-16之间,允许字母数字下划线"));
      }
      axios.findUserName({ userName: value }).then(({ data: res }) => {
        if (res.code !== "001") {
          return callback(new Error(res.msg));
        } else {
          callback();
        }
      });
    };
    // 密码验证
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      }
      if (!/^\w{6,16}[0-9,A-z]/.test(value)) {
        callback(new Error("字母开头,长度6-16之间,允许字母数字下划线"));
      }
      callback();
    };
    // 第二次密码验证
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.registerForm.password) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      dialogFormVisible: false,
      registerForm: {
        password: "",
        checkPass: "",
        userName: "",
      },

      // 验证规则
      sterFormRules: {
        password: [{ required: true, validator: validatePass, trigger: "blur" }],
        checkPass: [{ required: true, validator: validatePass2, trigger: "blur" }],
        userName: [{ required: true, validator: checkAge, trigger: "blur" }],
      },
    };
  },
  methods: {
    showDialog() {
      this.dialogFormVisible = true;
    },
    // 注册回调
    addRegister() {
      let arr = {
        password: this.registerForm.password,
        userName: this.registerForm.userName,
      };
      this.$refs.registerFormRef.validate(async (valid) => {
        if (!valid) return;
        let { data: res } = await axios.register(arr);
        console.log(res);
        if (res.code == "001") {
          this.$notify({
            title: "成功",
            message: res.msg,
            type: "success",
          });
          this.dialogFormVisible = false;
        } else {
          this.$notify.error({
            title: "失败",
            message: res.msg,
          });
        }
      });
    },
    // 关闭对话框回调  
    closeDia() {
      this.$refs.registerFormRef.resetFields();
    },
  },
};
</script>
<style lang="scss" scoped></style>
